
'''
TOPD:这是不用的request
'''
import requests
import urllib3
# from urllib3.exceptions import InsecureRequestWarning
from common.deal_with_response import deal_with_res

urllib3.disable_warnings()
# 加这句不会报错(requests证书警告)
#reguests.packages.urllib3.disable warnings(InsecureRequestWarning)
class HTTPRequests():
    def __init__ (self,url):
        self.url =url
        self.req =requests.session()
        # 依据自己公司的请求头默认值配置
        self.head ={
            'User-Agent':'Mlozi11a/5.0(Windows NT 6.2.W0W64;Trident/7.0;rv:11.0)1ike Gecko',
            'Accept':'image/gif,image/ipeg,image/pipeg,application/x-ms-application,application/xaml +xml,'
                       'application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint,'
                        'application/msword,*/*',
            'Accept-Language':'zh-CN'}

    # 封装自己的get请求，获取资源
    def get(self, uri='',params='',data='',headers=None, cookies=None, verify=False):
        if headers is None:
            headers = self.head
        # print("请求头是:{”.format(headers))
        url = self.url + uri
        res = self.req.get(url, params=params, data=data, headers=headers, cookies=cookies, verify = verify)
        deal_with_res(params, res)
        return res

    # 封装自己的post请求，获取资源
    def post(self, uri='',params='',data='',headers=None, cookies=None, verify=False):
        if headers is None:
            headers = self.head
        # print("请求头是:{”.format(headers))
        url = self.url + uri
        res = self.req.post(url, params=params, data=data, headers=headers, cookies=cookies, verify = verify)
        deal_with_res(params, res)
        return res

    # 封装自己的put请求，获取资源
    def put(self, uri='',params='',data='',headers=None, cookies=None, verify=False):
        if headers is None:
            headers = self.head
        # print("请求头是:{”.format(headers))
        url = self.url + uri
        res = self.req.put(url, params=params, data=data, headers=headers, cookies=cookies, verify = verify)
        deal_with_res(params, res)
        return res

     # 封装自己的delete请求，获取资源
    def delete(self, uri='', params='', data='', headers=None, cookies=None, verify=False):
        if headers is None:
            headers = self.head
        # print("请求头是:{”.format(headers))
        url = self.url + uri
        res = self.req.delete(url, params=params, data=data, headers=headers, cookies=cookies, verify=verify)
        deal_with_res(params, res)
        return res

# 测试一下下
if __name__ == '__main__':
    # 这里域名设置的是http://httpbin.org，懂得都懂
    get_res = HTTPRequests(url='http://httpbin.org').post("/get")
    post_res = HTTPRequests(url='http://httpbin.org').get("/post")
    print(get_res.text, "\n", post_res.text, "\n")